/**
 * Created by limeng on 16/5/25.
 */
delegateApp.controller("CheckProposalCtrl", function (
                $scope, $rootScope, $http, $routeParams) {
    $scope.proposalId = $routeParams.proposalId;
    $scope.proposal = {};
    $scope.secondMotionList = [];
    $scope.secondMotionPageNo = 1;
    $scope.showMoreMotion = false;

    $http.get('/proposal/'
        + $scope.proposalId
    ).success(
        function (data) {
            if (data.state == true) {
                $scope.proposal = data.proposal;
                var contentElem = document.getElementById("proposalContent");
                contentElem.innerHTML = $scope.proposal.content;
                contentElem.style.padding = 15;
            } else {
                showAlert('查询失败', '原因是:'+data.result);
            }
        }
    ).error(
        function () {
            showAlert('查询失败', '请联系开发者进行维护');
        }
    );

    $scope.refreshMotionList = function() {
        $http.get('/second_motion/' + $scope.proposalId
            + '/10/' + $scope.secondMotionPageNo
        ).success(
            function (data) {
                if (data.state == true) {
                    for (var i = 0; i < data.list.length; i++) {
                        $scope.secondMotionList.add(data.list[i]);
                    }
                    if ($scope.secondMotionPageNo <= data.pageCount) {
                        $scope.showMoreMotion = false;
                    } else {
                        $scope.showMoreMotion = true;
                    }
                } else {
                    showAlert('查询失败', '原因:'+data.result);
                }
            }
        ).error(
            function () {
                showAlert('查询失败', '附议信息数据库出现异常');
            }
        );
    };
    $scope.refreshMotionList();

    $scope.proposalPass = function () {
        bootbox.confirm('确定要通过审核吗？', function (result) {
            if (result == true) {
                $http.post('/proposal/checkPass/ ' + $scope.proposalId, {})
                    .success(function (data) {
                        if (data.status == true) {
                            bootbox.alert("已通过审核！", function () {
                                history.go(-1);
                            });
                        } else {
                            bootbox.alert("操作失败，原因是" + data.result);
                        }
                    }).error(function () {
                        bootbox.alert("服务器遇到故障！")
                    });
            }
        });
    };

    $scope.proposalReject = function () {
        bootbox.prompt('请输入拒绝的理由：', function (result) {
            if (result != null) {
                $http.post('/proposal/checkFailed/' + $scope.proposalId, result)
                    .success(function (data) {
                        if (data.status == true) {
                            bootbox.alert("已拒绝通过审核！", function () {
                                history.go(-1);
                            });
                        } else {
                            bootbox.alert("操作失败，原因是" + data.result);
                        }
                    }).error(function () {
                        bootbox.alert("服务器遇到故障！")
                    });
            }
        });
    }
});